/*
*
*  内容评论数据模型
*  负责内容评论部分数据
*
*/
const db = require('../../config/db')
module.exports = {
    // 编写添加评论方法
    // 参数：content_id, comment_content_message,from_user_id,to_user_id,comment_create_time,comment_content_id
    add(content_id, comment_content_message, from_user_id, to_user_id, comment_create_time, comment_content_id) {
        return new Promise((resolve, reject) => {
            db.query("insert into db_comment_content(content_id, comment_content_message,from_user_id,to_user_id,comment_create_time,comment_content_id) values('" + content_id + "','" + comment_content_message + "','" + from_user_id + "','" + to_user_id + "','" + comment_create_time + "','" + comment_content_id + "')", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '添加评论失败。' })
                } else {
                    resolve({ code: 200, msg: '添加评论成功。' })
                }
            })
        })
    },
    // 编写删除评论方法
    // 参数：comment_content_id
    remove(comment_content_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_comment_content where comment_content_id='" + comment_content_id + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '查询评论名称失败。' })
                } else {
                    db.query("delete from  db_comment_content where comment_content_id = '" + comment_content_id + "'", function (err, rows) {
                        if (err) {
                            resolve({ code: -1, msg: '删除评论失败。' })
                        } else {
                            resolve({ code: 200, msg: '删除评论成功。' })
                        }
                    })
                }
            })
        })
    },
    // 编写查询评论方法
    // 参数：content_id
    list(content_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_comment_content where content_id = '" + content_id + "' ", (err, rows) => {
                if (err) {
                    // 反馈信息给客户端
                    resolve({ code: -1, msg: '查询评论失败。' })
                } else {
                    resolve({ code: 200, msg: '查询评论成功。', result: rows })
                }
            })
        })
    },
    
    // 编写内容评论列表方法
    // 参数：page_num,page_size
    items(page_num, page_size) {
        return new Promise((resolve, reject) => {
            const params = [(parseInt(page_num) - 1) * parseInt(page_size), parseInt(page_size)]
            db.query("select * from db_comment_content limit " + params[0] + "," + params[1] + "", function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询内容评论失败。',
                        err
                    })
                } else {
                    //as更换名称
                    db.query('select count(*) as total from db_comment_content', function (error, among) {
                        if (error) {
                            resolve({
                                code: -1,
                                msg: '统计内容评论数量失败。',
                                error
                            })
                        } else {
                            resolve({
                                code: 200,
                                msg: "获取分页内容评论列表成功。",
                                result: rows,
                                page: {
                                    page_num: page_num,
                                    page_size: page_size,
                                    total: among[0]['total'] //查询表中的数量
                                }
                            })
                        }
                    })
                }
            })

        })
    },

    // 编写统计内容评论方法
    all() {
        return new Promise((resolve, reject) => {
            db.query('select count(*) as total from db_comment_content', function (error, among) {
                if (error) {
                    resolve({
                        code: -1,
                        msg: '统计内容评论数量失败。',
                        error
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取内容评论总数成功。",
                        total: among[0]['total']
                    })
                }
            })

        })
    },
}